package com.bf.hotpoint.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.bf.hotpoint.common.LogValues;
import com.bf.hotpoint.domain.Info;
import com.bf.hotpoint.domain.Log;
import com.bf.hotpoint.domain.Message;
import com.bf.hotpoint.domain.User;
import com.bf.hotpoint.repository.mongo.CommonMongoRepository;
import com.bf.hotpoint.repository.mongo.LogMongoRepository;
import com.bf.hotpoint.repository.redis.CommonRedisRepository;
import com.bf.hotpoint.repository.redis.UserRedisRepository;
import com.bf.hotpoint.service.CommonService;

@Service
public class CommonServiceImpl implements CommonService {
	
	static Logger log = Logger.getLogger(CommonServiceImpl.class.getName());
	
	@Autowired
	private CommonRedisRepository redisCommon;
	
	@Autowired
	private UserRedisRepository userRedis;
	
	@Autowired
	private CommonMongoRepository mongoCommon;
	
	@Autowired
	private LogMongoRepository logMongo;
	
	private Date getExpireDate(){
		Calendar c=new GregorianCalendar();
		c.add(Calendar.MONTH, 3);
		return c.getTime();
	}
	
	@PostConstruct
	public void initMongonAndRedis() {
		//init collections of mongo
		try {
			mongoCommon.initCollection(User.class);
			mongoCommon.initCollection(Message.class);
			mongoCommon.initCollection(Info.class);
			log.info("------------mongo init complete------------");	
		} catch (Exception e) {
			log.error("mongo init error", e);
			System.out.println(e.getMessage());
			System.out.println(e);
		}
		
		//init index of mongo
		try {
			mongoCommon.indexGeo("location", Message.class);
			
			
			/*set ttl,so availableTime don't set usual index
			 * 
			 * db.message.ensureIndex( { "availableTime": 1 }, { expireAfterSeconds: 3600 } )
			 * 
			 *mongoCommon.index("availableTime", Message.class,Direction.DESC); 
			 */
			mongoCommon.index("publishTime", Message.class,Direction.DESC);

			/*手动添加定时删除功能*/

			log.info("------------index of mongo init complete------------");	
		} catch (Exception e) {
			log.error("index of mongo init error", e);
			System.out.println(e.getMessage());
			System.out.println(e);
		}
		
		//init redis
		try {
			List<User> users = mongoCommon.findAllByClass(User.class);
			Map<String, User> userMap = new HashMap<String, User>();
			
			for(User user : users)
			{
				userMap.put(user.getUuid(), user);
			}
			
			userRedis.setUsers(userMap, this.getExpireDate());
			log.info("------------redis init complete------------");	
		} catch (Exception e) {
			log.error("redis init error", e);
			System.out.println(e.getMessage());
			System.out.println(e);
		}
	}
	
	public void saveLog()
	{
		Log log = new Log();
		log.setCommentSum(LogValues.commentSum);
		log.setDate(new Date());
		log.setSixtyMinsSum(LogValues.sixtyMinsSum);
		log.setTenMinsSum(LogValues.tenMinsSum);
		log.setThirtyMinsSum(LogValues.thirtyMinsSum);
		
		logMongo.addLog(log);
		LogValues.commentSum = 0;
		LogValues.sixtyMinsSum = 0;
		LogValues.tenMinsSum = 0;
		LogValues.thirtyMinsSum = 0;
	}
}
